package com.yyfax.ph.tools;

import android.app.Activity;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListAdapter;

import static android.view.HapticFeedbackConstants.LONG_PRESS;

/**点击事件公共类
 * Created by xiongyufeng on 2017/10/16.
 * Copyright  2017 www.yylending.com. All Rights Reserved.
 */
public class UiActions {
	public static boolean clickOn(View view) {
		return view.performClick();
	}

	public static boolean clickOn(Activity activity, int targetViewId) {
		return activity.findViewById(targetViewId).performClick();
	}

	public static boolean clickOn(View parentView, int targetViewId) {
		return parentView.findViewById(targetViewId).performClick();
	}

	public static boolean clickOn(Fragment fragment, int targetViewId) {
		return fragment.getView().findViewById(targetViewId).performClick();
	}

	public static void pressBackButton(Activity currentActivity) {
		if (currentActivity.getFragmentManager().getBackStackEntryCount() > 0)
			currentActivity.getFragmentManager().popBackStackImmediate();
		else
			currentActivity.onBackPressed();
	}

	public static void clickItem(AbsListView listView, int position) {
		ListAdapter adapter = listView.getAdapter();
		View itemView = adapter.getView(position, null, listView);
		listView.performItemClick(itemView, position, adapter.getItemId(position));
	}

	public static void clickItem(AbsListView listView, int position, int viewId) {
		ListAdapter adapter = listView.getAdapter();
		View itemView = adapter.getView(position, null, listView);
		itemView.findViewById(viewId).performClick();
	}

	public static void longClickItem(AbsListView listView, int position) {
		if (!listView.isLongClickable())
			return;
		AdapterView.OnItemLongClickListener listener = listView.getOnItemLongClickListener();
		if (listener == null)
			return;
		ListAdapter adapter = listView.getAdapter();
		View itemView = adapter.getView(position, null, listView);
		listener.onItemLongClick(listView, itemView, position, adapter.getItemId(position));
		listView.performHapticFeedback(LONG_PRESS);
	}


}